package xixirupan.tmgp.tencent.com.vmeim.login.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.bmob.sms.BmobSMS;
import cn.bmob.sms.exception.BmobException;
import cn.bmob.sms.listener.RequestSMSCodeListener;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;
import xixirupan.tmgp.tencent.com.vmeim.R;
import xixirupan.tmgp.tencent.com.vmeim.base.BaseActivity;
import xixirupan.tmgp.tencent.com.vmeim.login.presenter.IPostData;

public class RegistorActivity extends BaseActivity {

    @BindView(R.id.edit_codelogin)
    EditText editCodelogin;
    @BindView(R.id.loginbutton)
    Button loginbutton;
    private Context context;

    @BindView(R.id.registor_goback)
    ImageView registorGoback;
    @BindView(R.id.acronmy_distribute)
    TextView acronmyDistribute;
    @BindView(R.id.select_distribute)
    ImageView selectDistribute;
    @BindView(R.id.phonepreffix)
    TextView phonepreffix;
    @BindView(R.id.registor_input)
    EditText registorInput;
    @BindView(R.id.line_input)
    LinearLayout lineInput;
    @BindView(R.id.registor_button)
    Button registorButton;
    @BindView(R.id.link_userxieyi)
    TextView linkUserxieyi;
    @BindView(R.id.activity_registor)
    RelativeLayout activityRegistor;
    @BindView(R.id.big_gou)
    ImageView bigGou;
    @BindView(R.id.flage)
    TextView flage;
    @BindView(R.id.rectop)
    RelativeLayout rectop;
    @BindView(R.id.line_s)
    TextView lineS;
    private Unbinder bind;
    private IViewPost viewPost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registor);
        bind = ButterKnife.bind(this);
        context = this;
        //初始化bmob云
        Bmob.initialize(context, "fd1bbae51d51f748e4dcb98e6e8ae7b1");
        //初始胡Bmob云的短信功能
        BmobSMS.initialize(context, "fd1bbae51d51f748e4dcb98e6e8ae7b1");

    }


    @OnClick({R.id.registor_button, R.id.link_userxieyi, R.id.select_distribute})
    public void onClick(final View view) {
        switch (view.getId()) {
            //注册按钮
            case R.id.registor_button:
                final String number = registorInput.getText() + "";
                //点击注册 跳转到完成注册页面
                //请求验证码 参数一上下文环境 参数二电话号码 参数三短信模板名称 参数四接口
                BmobSMS.requestSMSCode(context, number, "meishi", new RequestSMSCodeListener() {
                            @Override
                            public void done(Integer integer, BmobException e) {
                                if (e == null) {
                                    //注册请求验证码请求成功

                                    Intent intent = new Intent(context, CompleteRegistorActivity.class);

                                    intent.putExtra("phonenum", number);
                                    startActivity(intent);
                                } else {
                                    //注册请求验证码请求成功
                                    Log.i("注册请求验证码请求验证码发送失败", "原因===>" + e.getMessage());
                                    String message = e.getMessage();
                                    Snackbar.make(view, message + "发送失败", Snackbar.LENGTH_SHORT).show();
//                                    Intent intent =new Intent(context,CompleteRegistorActivity.class);
//
//                                    intent.putExtra("phonenum",number);
//                                    startActivity(intent);
                                }
                            }
                        }
                );

                break;
            //选择地区 弹出popowindow
            case R.id.select_distribute:

                //实例化SelectPicPopupWindow
                SelectPoupWindow window = new SelectPoupWindow(RegistorActivity.this, new IPostData() {
                    @Override
                    public void postTextData(String name, String ename) {
                        acronmyDistribute.setText(name);
                        phonepreffix.setText("+" + ename);

                    }
                });
                //显示窗口
                window.showAtLocation(RegistorActivity.this
                        //设置layout在PopupWindow中显示的位置
                        .findViewById(R.id.activity_registor), Gravity.BOTTOM | Gravity
                        .CENTER_HORIZONTAL, 0, 0);
                break;
            //用户协议链接
            case R.id.link_userxieyi:


                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }


    @OnClick(R.id.loginbutton)
    public void onClick() {
        BmobUser user =new BmobUser();
        final String phonenum= registorInput.getText()+"";
        user.setMobilePhoneNumber(phonenum);
        String rcode =editCodelogin.getText()+"";
        Log.i(";;;;;;","--"+phonenum+";;"+rcode);
        user.signOrLogin(rcode, new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser user, cn.bmob.v3.exception.BmobException e) {
                if (e == null)
                {

                    Log.i("TAG","---->注册或者登录成功");
                    //转到登录页面
                    Intent intent =new Intent(context, LoginSucessActivity.class);
                    intent.putExtra("account",phonenum);
                    startActivity(intent);
                }else {
                    Log.i("TAG","_-------->注册失败原因"+e.getMessage());
                }
            }
        });

    }
}
